package zhihui.mobile.ui.address.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.address.UiData;
import zhihui.mobile.presenter.address.AddressPresenter;
import zhihui.mobile.ui.address.AddressActivity;

/**
 * Created by 张公铭 on 2017/7/6 0006.
 */

public class AreaAdapter extends BaseAdapter
{
    private Context mContext;
    private List<UiData> mList = new ArrayList<>();
    private LayoutInflater inflater;
    private String curId = "";
    private IAdapterLisenter listener;

    public interface IAdapterLisenter
    {
        void initLastView(View view);
    }

    public void setListener(IAdapterLisenter listener)
    {
        this.listener = listener;
    }

    public AreaAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmList(List<? extends UiData> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    public void setCurId(String curId)
    {
        this.curId = curId;
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_text;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.address_area_item, null);
            tv_text = (TextView) convertView.findViewById(R.id.tv_address_area_item);

            ViewCache viewCache = new ViewCache();
            viewCache.tv_text = tv_text;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_text = viewCache.tv_text;
        }

        if (mList != null && mList.get(position) != null)
        {
            tv_text.setText(mList.get(position).getName());
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
            if (curId.equals(mList.get(position).getId()))
            {
                convertView.setBackgroundColor(Color.parseColor("#f5f5f5"));
                if (listener != null)
                {
                    listener.initLastView(convertView);
                }
            }
        }
        return convertView;
    }

    class ViewCache
    {
        public TextView tv_text;
    }
}
